package com.tyj.movie.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {



    /**
     * 配置返回类型为 json
     * @return
     */
    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
        RedisCacheConfiguration config= RedisCacheConfiguration.defaultCacheConfig();
        //设置springCache集成redis时 需要配置redis的数据存储格式
        config=config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config=config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

        //加载自己的配置文件
        CacheProperties.Redis redisProperties=cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }


    //配置指定存活时间
//    @Bean
//    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate, RedisCacheConfiguration redisCacheConfiguration) {
//        // 够着一个redis缓存管理器
//        RedisCacheManager redisCacheManager =
//                RedisCacheManager.RedisCacheManagerBuilder
//                        // Redis 连接工厂
//                        .fromConnectionFactory(redisTemplate.getConnectionFactory())
//                        // 缓存配置
//                        .cacheDefaults(redisCacheConfiguration)
//                        //指定缓存名 存活时间
//                        .withCacheConfiguration("movie:detail", getCacheConfigurationWithTtl(redisTemplate, 1000))
//
//                        // 配置同步修改或删除 put/evict
//                        .build();
//
//        return redisCacheManager;
//    }
//
//    RedisCacheConfiguration getCacheConfigurationWithTtl(RedisTemplate<String, Object> template, long seconds) {
//
//        return RedisCacheConfiguration
//                .defaultCacheConfig()
//                // 设置key为String
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getStringSerializer()))
//                // 设置value 为自动转Json的Object
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getValueSerializer()))
//                // 不缓存null
//                .disableCachingNullValues()
//                // 缓存数据保存1小时
//                .entryTtl(Duration.ofSeconds(seconds));
//    }

}
